package gota.processingResources;

import java.io.File;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import gate.Factory;
import gate.FeatureMap;
import gate.creole.SerialAnalyserController;

import org.w3c.dom.Node;

public class RegExSplitterResource implements IProcessingResource {

	@Override
	public String getResourceName() {
		return "RegExSplitter";
	}

	@Override
	public void init(SerialAnalyserController controller, Node node)
			throws Exception {
		XPath xpath = XPathFactory.newInstance().newXPath();
		
		org.w3c.dom.Node nonSplitListNode = (org.w3c.dom.Node) xpath.evaluate(
				"//NonSplitListUrl", node, XPathConstants.NODE);
		org.w3c.dom.Node externalListNode = (org.w3c.dom.Node) xpath.evaluate(
				"//ExternalSplitListUrl", node, XPathConstants.NODE);
		org.w3c.dom.Node internalListNode = (org.w3c.dom.Node) xpath.evaluate(
				"//InternalSplitListUrl", node, XPathConstants.NODE);
		String internalList = internalListNode.getTextContent();
		String externalList = externalListNode.getTextContent();
		String nonSplitList = nonSplitListNode.getTextContent();

		FeatureMap params = Factory.newFeatureMap();
		params.put("internalSplitListURL", new File(internalList).toURI()
				.toURL());
		params.put("externalSplitListURL", new File(externalList).toURI()
				.toURL());
		params
				.put(
						gate.creole.splitter.RegexSentenceSplitter.SPLIT_NON_SPLIT_LIST_PARAMETER_NAME,
						new File(nonSplitList).toURI().toURL());
		params
				.put(
						gate.creole.splitter.RegexSentenceSplitter.SPLIT_ENCODING_PARAMETER_NAME,
						"UTF-8");

		FeatureMap features = Factory.newFeatureMap();
		gate.creole.splitter.RegexSentenceSplitter regExSplitter = (gate.creole.splitter.RegexSentenceSplitter) Factory
				.createResource("gate.creole.splitter.RegexSentenceSplitter",
						params, features);

		controller.add(regExSplitter);
	}

}
